<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Passenger
 * Desc  乘车人控制器
 * Create on 2024/10/22 10:32
 * Create by wangyafang
 */

namespace app\api\controller\wddj\user;


use app\api\model\wddj\User;
use app\common\controller\Api;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Passenger extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $model = null;
    protected $wechat_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\user\Passenger();
        $this->wechat_id = User::getWechatId($this->auth->id);
        if (empty($this->wechat_id)) {
            $this->error('用户信息不存在');
        }
    }

    /**
     * Desc 乘车人列表
     * Create on 2024/10/22 10:34
     * Create by wangyafang
     */
    public function index()
    {
        if (!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);
        $where['wechat_id'] = array('eq',$wechat_id);

        $order = 'id desc';

        $data = $this->model
            ->where($where)
            ->field("id,name,mobile")
            ->order($order)
            ->select();

        foreach ($data as &$v) {
            $v->mobile = substr_replace($v->mobile, '*****', 3, -3);
        }

        $this->success('请求成功',$data);
    }
    
    /**
     * Desc 添加
     * Create on 2024/10/22 10:34
     * Create by wangyafang
     */
    public function add()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);

        $param = $this->request->post();
        $create_data = array(
            'wechat_id'=>$wechat_id,
        );
        $create_data = array_merge($create_data,$param);
        $result = $this->validate($create_data,'app\api\validate\wddj\user\Passenger');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }


        Db::startTrans();
        try {
            $this->model->data($create_data);
            $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('添加成功');
    }
    
    /**
     * Desc 编辑
     * Create on 2024/10/22 10:34
     * Create by wangyafang
     */
    public function edit()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $param = $this->request->post();
        $id = $param['id'];
        $passengerObj = $this->model->get($id);
        if (!$passengerObj) {
            $this->error('乘车人信息不存在');
        }
        $param['wechat_id'] = $passengerObj['wechat_id'];
        $result = $this->validate($param,'app\api\validate\wddj\user\Passenger');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $wechat_id = User::getWechatId($this->auth->id);
        if ($passengerObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }


        Db::startTrans();
        try {
            $passengerObj->save($param);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('编辑成功');
    }
    
    /**
     * Desc 删除
     * Create on 2024/10/22 10:35
     * Create by wangyafang
     */
    public function del()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $id = $this->request->post('id');
        $passengerObj = $this->model->get($id);
        if (!$passengerObj) {
            $this->error('乘车人信息不存在');
        }

        $wechat_id = User::getWechatId($this->auth->id);
        if ($passengerObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法操作');
        }

        $result = false;
        Db::startTrans();
        try {
            $result = $passengerObj->delete();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if ($result == false) {
            $this->error('删除失败');
        }

        $this->success('删除成功');
    }
    


    /**
     * Desc 详情
     * Create on 2024/9/26 14:38
     * Create by wangyafang
     */
    public function details()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $id = $this->request->get('id');
        $passengerObj = $this->model->where('id',$id)->field('id,name,mobile,wechat_id')->find();
        $wechat_id = User::getWechatId($this->auth->id);
        if ($passengerObj['wechat_id'] != $wechat_id) {
            $this->error('不是本人，无法查看');
        }
        $passengerObj->hidden(['wechat_id']);

        $this->success('请求成功',$passengerObj);
    }


}



 